import { createRouter, createWebHistory } from "vue-router";
import Main from "../views/Main";
import Login from "../views/Login";
import CategoryEdit from "../views/CategoryEdit";
import CategoryList from "../views/CategoryList";
import ItmeEdit from "../views/ItmeEdit";
import ItemList from "../views/ItemList";
import HeroEdit from "../views/HeroEdit";
import HeroList from "../views/HeroList";
import ArticleEdit from "../views/ArticleEdit";
import ArticleList from "../views/ArticleList";
import AdsEdit from "../views/AdsEdit";
import AdsList from "../views/AdsList";
import AdminUserEdit from "../views/AdminUserEdit";
import AdminUserList from "../views/AdminUserList";
import Namedes from "../views/namedes";

const routes = [
  {
    path: "/login",
    name:"login",
    component:Login,meta:{isPublic:true}
  },
  {
    path: "/",
    name: "main",
    component: Main,
    children: [
      // 分类
      { path: "/categories/create", component: CategoryEdit },
      { path: "/categories/edit/:id", component: CategoryEdit, props: true },
      { path: "/categories/list", component: CategoryList },
      // 物品
      { path: "/items/create", component: ItmeEdit },
      { path: "/items/edit/:id", component: ItmeEdit, props: true },
      { path: "/items/list", component: ItemList },
      // 英雄
      { path: "/heroes/create", component: HeroEdit },
      { path: "/heroes/edit/:id", component: HeroEdit, props: true },
      { path: "/heroes/list", component: HeroList },
      // 文字
      { path: "/articles/create", component: ArticleEdit },
      { path: "/articles/edit/:id", component: ArticleEdit, props: true },
      { path: "/articles/list", component: ArticleList },
      // 广告
      { path: "/ads/create", component: AdsEdit },
      { path: "/ads/edit/:id", component: AdsEdit, props: true },
      { path: "/ads/list", component: AdsList },
      // 管理员
      { path: "/admin_user/create", component: AdminUserEdit },
      { path: "/admin_user/edit/:id", component: AdminUserEdit, props: true },
      { path: "/admin_user/list", component: AdminUserList },
      //
      { path: "/namedes/list", component: Namedes },
    ],
  },
];
const router = createRouter({
  history: createWebHistory(),
  routes,
});
// 添加路由守卫
router.beforeEach((to, from, next) => {
  if(!to.meta && !localStorage.token){
    return next('/login');
  }
  next()
});
export default router;
